import { get as _get } from 'object-path';
import {
    getOrgList,
    getOrgById,
    updateOrgInfo,
    getOrgTree,
    getOrgInfoDetail,
    getRegionalLeader
} from '@/services/Organization/orgmanage';
import { deleteNullChildren } from '@/utils/utils';
import { addCEO } from './OrgsViewModel';

export default {
    namespace: 'orgManage',
    state: {
        data: [],
        pagination: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        orgInfo: {
            id: null,
            companyName: null,
            business: null,
            companyAddress: null,
            companyArea: null,
            companyEmail: null,
            companyManager: null,
            companyPhone: null,
            industry: null,
            introduce: null,
            legalRepresentative: null,
            licenseCode: null,
            parentId: null,
            regTime: null
        },
        orgInfoDetail: {},
        orgTree: [],
        regionalLeader: []
    },
    effects: {
        // eslint-disable-next-line no-unused-vars
        *fetchOrgList({ payload }, { put, call }) {
            const result = yield call(getOrgList, payload);
            if (result && result.code === 10000) {
                const orgListData = deleteNullChildren(_get(result, 'data', []));
                const orgList = {
                    data: orgListData
                };
                yield put({
                    type: 'setOrgList',
                    payload: orgList
                });
            }
        },
        *fetchOrgInfo({ payload }, { put, call }) {
            const result = yield call(getOrgById, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'setOrgInfo',
                    payload: result.data
                });
            }
        },
        *fetchOrgTree(_, { put, call }) {
            const result = yield call(getOrgTree);
            if (result && result.code === 10000) {
                const orgTree = {
                    orgTree: _get(result, 'data', [])
                };
                yield put({
                    type: 'setOrgTree',
                    payload: orgTree
                });
            }
        },
        *fetchOrgInfoDetail({ payload }, { put, call }) {
            const result = yield call(getOrgInfoDetail, payload);
            if (result && result.code === 10000) {
                const orgInfoDetail = {
                    orgInfoDetail: {
                        ..._get(result, 'data', {}),
                        company: addCEO(result)
                    }
                };
                yield put({
                    type: 'setOrgInfoDetail',
                    payload: orgInfoDetail
                });
            }
        },
        *saveOrgInfo({ payload, callback }, { call }) {
            const response = yield call(updateOrgInfo, payload);
            if (callback) callback(response);
        },
        *fetchRegionalLeader({ payload }, { put, call }) {
            const result = yield call(getRegionalLeader, payload);
            if (result && result.code === 10000) {
                const regionalLeader = {
                    regionalLeader: _get(result, 'data', [])
                };
                yield put({
                    type: 'setRegionalLeader',
                    payload: regionalLeader
                });
            }
        }
    },
    reducers: {
        setOrgList(state, action) {
            return {
                ...state,
                pagination: {
                    current: action.payload.current || 1,
                    pageSize: action.payload.pageSize || 10
                },
                ...action.payload
            };
        },
        setOrgInfo(state, action) {
            return {
                ...state,
                orgInfo: { ...action.payload }
            };
        },
        setOrgTree(state, action) {
            return {
                ...state,
                ...action.payload
            };
        },
        setOrgInfoDetail(state, action) {
            return {
                ...state,
                ...action.payload
            };
        },
        setPagination(state, { pagination }) {
            return {
                ...state,
                pagination
            };
        },
        setRegionalLeader(state, action) {
            return {
                ...state,
                ...action.payload
            };
        }
    }
};
